package com.wicket.okrframework.base.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wicket.okrframework.base.service.MSceneService;
import com.wicket.okrframework.base.service.dto.req.QuerySceneDetailReq;
import com.wicket.okrframework.base.service.dto.req.QuerySceneListReq;
import com.wicket.okrframework.common.dto.FieldInfo;
import com.wicket.okrframework.common.exception.BizException;
import com.wicket.okrframework.common.tools.StringUtils;
import com.wicket.okrframework.common.util.CommonFunctionHelper;
import com.wicket.okrframework.dal.mapper.mbg.SceneMapper;
import com.wicket.okrframework.dal.po.mbg.Scene;
import org.apache.skywalking.apm.toolkit.trace.Tag;
import org.apache.skywalking.apm.toolkit.trace.Tags;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.ArrayList;

/**
 * @author ：
 * @since 2022/1/19 15:39
 *
 *
 * @version 1.0
 */
@Service
public class MSceneServiceImpl
        extends ServiceImpl<SceneMapper, Scene>
        implements MSceneService {

    @Autowired
    SceneMapper sceneMapper;

    /**
   * 1-2-07查询场景详情[169]
   * gen by moon at 5/6/2023, 2:53:59 AM
   */
 @Trace(operationName = "1-2-07查询场景详情")
 @Tags({@Tag(key = "参数",value = "arg[0]"),
 @Tag(key = "返回值",value = "returnedObj")})
  @Override
  @Transactional(rollbackFor=Exception.class)
  public  Scene  querySceneDetail(QuerySceneDetailReq reqDto) {
     
        try {
         reqDto.setIsValid("FALSE");
            QueryWrapper<Scene> queryWrapper = new QueryWrapper<>();
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, new ArrayList<>());
            queryWrapper.orderByDesc("create_time");
            List<Scene> retList = super.list(queryWrapper);
            if(retList!=null && retList.size()>0){
                Scene retData = retList.get(0);
                retData.setSceneLogo(CommonFunctionHelper.getFilePath(retData.getSceneLogo()));//otherModel: ALL_PATH
                return retData;
            }
            return null;
      } catch (Exception e) {
          e.printStackTrace();
          throw new BizException("-1",e,false);
      }
  }

    /**
   * 1-2-07查询场景列表[683]
   * gen by moon at 10/23/2022, 2:25:48 PM
   */
 @Trace(operationName = "1-2-07查询场景列表")
 @Tags({@Tag(key = "参数",value = "arg[0]"),
 @Tag(key = "返回值",value = "returnedObj")})
  @Override
  @Transactional(rollbackFor=Exception.class)
  public  List<Scene>  querySceneList(QuerySceneListReq reqDto) {
     
        try {
            QueryWrapper<Scene> queryWrapper = new QueryWrapper<>();
            reqDto.setIsValid("FALSE");
            
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(Scene.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(Scene.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper,likeList,fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap,likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(Scene.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);
            
            return ((List<Scene>) super.list(queryWrapper)).stream().map(item->{
                 item.setSceneLogo(CommonFunctionHelper.getFilePath(item.getSceneLogo()));//otherModel: ALL_PATH
                return item;
            }).collect(Collectors.toList());
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1",e,false);
        }
  }

    /**
   * 1-2-07删除场景--待删除[681]
   * gen by moon at 10/23/2022, 2:25:47 PM
   */
 @Trace(operationName = "1-2-07删除场景--待删除")
 @Tags({@Tag(key = "参数",value = "arg[0]"),
 @Tag(key = "返回值",value = "returnedObj")})
  @Override
  @Transactional(rollbackFor=Exception.class)
  public  boolean  deleteScene(String sceneId ) {
     
      return super.removeById(sceneId);
  }

    /**
   * 1-2-07修改场景[70]
   * gen by moon at 10/23/2022, 2:25:46 PM
   */
 @Trace(operationName = "1-2-07修改场景")
 @Tags({@Tag(key = "参数",value = "arg[0]"),
 @Tag(key = "返回值",value = "returnedObj")})
  @Override
  @Transactional(rollbackFor=Exception.class)
  public  boolean  updateScene(Scene scene) {
//     // TODO: 2024/1/20
//     if(!StringUtils.isEmpty(scene.getSceneLogo())){
//         scene.setSceneLogo(CommonFunctionHelper.replaceFilePath(scene.getSceneLogo()));
//     }
     scene.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
if(scene!=null && scene.getSubjectLifeCycle()!=null && scene.getSubjectLifeCycle().equals("CURRENTLY_VALID")){
             scene.setOperateTime(CommonFunctionHelper.getCurrentTime());//RELEASE_SYSTEM_CURRENT_TIME
            }
if(scene!=null && scene.getSubjectLifeCycle()!=null &&  scene.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            scene.setReleaseInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//RELEASED_CURRENT_INDUCTIONID
          }
if(scene!=null && scene.getSubjectLifeCycle()!=null && scene.getSubjectLifeCycle().equals("CURRENTLY_VALID")){
             scene.setReleaseTime(CommonFunctionHelper.getCurrentTime());//RELEASE_SYSTEM_CURRENT_TIME
            }
        return super.updateById(scene);
  }

    /**
   * 1-2-07新增场景[69]
   * gen by moon at 10/23/2022, 2:25:46 PM
   */
 @Trace(operationName = "1-2-07新增场景")
 @Tags({@Tag(key = "参数",value = "arg[0]"),
 @Tag(key = "返回值",value = "returnedObj")})
  @Override
  @Transactional(rollbackFor=Exception.class)
  public  String  addScene(Scene scene) {
//     // TODO: 2024/1/20
//     if(!StringUtils.isEmpty(scene.getSceneLogo())){
//         scene.setSceneLogo(CommonFunctionHelper.replaceFilePath(scene.getSceneLogo()));
//     }
     scene.setSceneId(CommonFunctionHelper.getUid());//DISTRIBUTED_ID
scene.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
scene.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
scene.setIsValid("FALSE");
scene.setIsArchive("FALSE");
scene.setCreateInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
scene.setCreateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
scene.setSpaceId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecSpaceId());//CURRENT_SPACEID
scene.setAppId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecAppId());//CURRENT_APPID
if(scene!=null && scene.getSubjectLifeCycle()!=null &&  scene.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            scene.setReleaseInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//RELEASED_CURRENT_INDUCTIONID
          }
if(scene!=null && scene.getSubjectLifeCycle()!=null && scene.getSubjectLifeCycle().equals("CURRENTLY_VALID")){
             scene.setReleaseTime(CommonFunctionHelper.getCurrentTime());//RELEASE_SYSTEM_CURRENT_TIME
            }
     if(super.save(scene)){
        return scene.getSceneId();
     }else {
        return "-1";
     }
    
  }
}
